package com.example.newsapp.ui.components.mainComponent

import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.Icon
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.compose.currentBackStackEntryAsState
import com.example.newsapp.data.compositionLocal.LocalDrawerEnabled
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.list.pageList
import com.example.newsapp.data.`object`.StateSave
import com.example.newsapp.ui.components.otherComponent.drawerComponent.DrawContext
import com.example.newsapp.ui.navigation.RuleNavigation
import kotlinx.coroutines.CoroutineScope

/**
 * 侧边导航栏
 *
 * @param paddingValue 内边距
 * @param scope 启动协程
 */
@Composable
fun DrawerNavigationBar(
    paddingValue: PaddingValues,
    scope: CoroutineScope,
    modifier: Modifier = Modifier
) {
    // 抽屉式导航栏选择
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    // 获取堆栈导航当前页
    var currentDestination = LocalNavController.current.currentBackStackEntryAsState().value?.destination
    // 抽屉式导航栏手势开关
    var gesturesEnable by rememberSaveable { mutableStateOf(true) }

    CompositionLocalProvider(
        LocalDrawerEnabled provides drawerState,
    ) {
        ModalNavigationDrawer(
            drawerState = drawerState,
            drawerContent = {
                // 侧边栏内容
                DrawContext()
            },
            gesturesEnabled = StateSave.gesturesEnable
        ) {
            Box(modifier = Modifier.padding(paddingValue).fillMaxSize()) {
                // 导航图
                RuleNavigation()
                AnimatedVisibility (currentDestination?.route in pageList) {
                    // 顶部菜单按钮
                    TopMenuBar(scope = scope)
                }
            }
        }
    }
}